package com.demo2do.boyle.common.entity.customer;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import com.alibaba.fastjson.annotation.JSONField;
import com.demo2do.boyle.common.entity.system.tag.Tag;

/**
 * 
 * @author Wilson
 */
@Entity
@Table(name = "customer_tag")
public class CustomerTag {

    @Id
    @GeneratedValue(generator = "gen")
    @GenericGenerator(name = "gen", strategy = "foreign", parameters = @Parameter( name = "property", value = "customer"))
    private Integer id;
    
    @OneToOne(fetch = FetchType.LAZY)
	@PrimaryKeyJoinColumn
	@JSONField(serialize = false)
    private Customer customer;
    
    private int lifeTag;
    
    private int interestTag;
    
    private int areaTag;
    
    private int sexTag;
    
    private int ageTag;
    
    
    /**
     * default constructor
     */
    public CustomerTag() {
    	
    }
    
    /**
     * @param customer
     */
    public CustomerTag(Customer customer) {
    	this.customer = customer;
    	this.lifeTag = 0;
    	this.interestTag = 0;
    	this.areaTag = 0;
    	this.sexTag = 1;
    	this.ageTag = 0;
    }
    
    /**
     * 
     * @param ageTag
     * @param areaTag
     * @param sexTag
     * @param lifeTagCodes
     * @param interestTagCodes
     * @return
     */
    public CustomerTag initialize(Tag ageTag, Tag areaTag, Tag sexTag, List<Integer> lifeTagCodes, List<Integer> interestTagCodes) {
    	this.ageTag = ageTag.getCode();
    	this.areaTag = areaTag.getCode();
    	this.sexTag = sexTag.getCode();
    	this.lifeTag = this.sumCode(lifeTagCodes);
    	this.interestTag = this.sumCode(interestTagCodes);
    	return this;
    }
    
    /**
     * 
     * @param ageTagCode
     * @param areaTagCode
     * @param sexTagCode
     * @param lifeTagCodes
     * @param interestTagCodes
     */
    public void update(Integer ageTagCode, Integer areaTagCode, Integer sexTagCode, List<Integer> lifeTagCodes, List<Integer> interestTagCodes) {
    	this.ageTag = ageTagCode;
    	this.areaTag = areaTagCode;
    	this.sexTag = sexTagCode;
    	this.lifeTag = this.sumCode(lifeTagCodes);
    	this.interestTag = this.sumCode(interestTagCodes);
    }
    
    /**
     * 
     * @param tagCodes
     * @return
     */
    public Integer sumCode(List<Integer> tagCodes) {
    	int tagCode = 0;
    	for(Integer code : tagCodes) {
    		tagCode = tagCode + code;
    	}
    	return tagCode;
    }
    
    public List<Integer> splitCode(Integer tagCode) {
    	
    	List<Integer> tagCodes = new ArrayList<Integer>();
    	
    	for(Integer pow = 1; pow != 0; pow <<= 1) {
			if((tagCode & pow) != 0) {
				tagCodes.add(pow);
			}
		}
		return tagCodes;
    }
    
    public List<String> splitCodeToString(Integer tagCode) {
    	List<String> tagCodes = new ArrayList<String>();
    	
    	for(Integer pow = 1; pow != 0; pow <<= 1) {
			if((tagCode & pow) != 0) {
				tagCodes.add(pow.toString());
			}
		}
		return tagCodes;
    }
    
    /**
     * 
     * @param tagCode
     * @return
     */
    public CustomerTag addInterestTag(Integer tagCode) {
    	List<Integer> interestTagCodes= this.getInterestTagCodes();
    	interestTagCodes.add(new Integer(tagCode));
    	this.interestTag = this.sumCode(interestTagCodes);
    	return this;
    }
    
    /**
     * 
     * @param tagCode
     * @return
     */
    public CustomerTag addLifeTag(Integer tagCode) {
    	List<Integer> lifeTagCodes= this.getLifeTagCodes();
    	lifeTagCodes.add(new Integer(tagCode));
    	this.lifeTag = this.sumCode(lifeTagCodes);
    	return this;
    }
    
    /**
     * 
     * @param tagCode
     * @return
     */
    public CustomerTag deleteInterestTag(Integer tagCode) {
    	List<Integer> interestTagCodes= this.getInterestTagCodes();
    	interestTagCodes.remove(new Integer(tagCode));
    	this.interestTag = this.sumCode(interestTagCodes);
    	return this;
    }
    
    /**
     * 
     * @param tagCode
     * @return
     */
    public CustomerTag deleteLifeTag(Integer tagCode) {
    	List<Integer> lifeTagCodes= this.getLifeTagCodes();
    	lifeTagCodes.remove(new Integer(tagCode));
    	this.lifeTag = this.sumCode(lifeTagCodes);
    	return this;
    }
    
    /**
     * @return the life tag value list
     */
    public List<Integer> getLifeTagCodes() {
		return this.splitCode(this.lifeTag);
    }
    
    /**
     * @return the life tag value list
     */
    public List<String> getStringLifeTagCodes() {
		return this.splitCodeToString(this.lifeTag);
    }
    
    /**
     * @return the interest tag value list
     */
    public List<Integer> getInterestTagCodes() {
		return this.splitCode(this.interestTag);
    }
    
    /**
     * @return the interest tag value list
     */
    public List<String> getStringInterestTagCodes() {
		return this.splitCodeToString(this.interestTag);
    }
    
    /**
	 * @return the sexTag
	 */
	public String getStringSexTag() {
		return String.valueOf(this.sexTag);
	}
	
	/**
	 * @return the areaTag
	 */
	public String getStringAreaTag() {
		return String.valueOf(this.areaTag);
	}
	
	/**
	 * @return the areaTag
	 */
	public String getStringAgeTag() {
		return String.valueOf(this.ageTag);
	}

    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }


	/**
	 * @return the lifeTag
	 */
	public int getLifeTag() {
		return lifeTag;
	}

	/**
	 * @return the interestTag
	 */
	public int getInterestTag() {
		return interestTag;
	}


	/**
	 * @return the areaTag
	 */
	public int getAreaTag() {
		return areaTag;
	}


	/**
	 * @return the sexTag
	 */
	public int getSexTag() {
		return sexTag;
	}


	/**
	 * @return the ageTag
	 */
	public int getAgeTag() {
		return ageTag;
	}


	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}


	/**
	 * @param customer the customer to set
	 */
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}


	/**
	 * @param lifeTag the lifeTag to set
	 */
	public void setLifeTag(int lifeTag) {
		this.lifeTag = lifeTag;
	}


	/**
	 * @param interestTag the interestTag to set
	 */
	public void setInterestTag(int interestTag) {
		this.interestTag = interestTag;
	}


	/**
	 * @param areaTag the areaTag to set
	 */
	public void setAreaTag(int areaTag) {
		this.areaTag = areaTag;
	}


	/**
	 * @param sexTag the sexTag to set
	 */
	public void setSexTag(int sexTag) {
		this.sexTag = sexTag;
	}


	/**
	 * @param ageTag the ageTag to set
	 */
	public void setAgeTag(int ageTag) {
		this.ageTag = ageTag;
	}
    
}
